package services

import (
	"errors"
	"fmt"
	"strconv"

	"cashew.com/cashew_common/constants"
	"cashew.com/cashew_common/utils"
	"cashew.com/cashew_match/dtos"
	"cashew.com/cashew_match/models"
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/logs"
)

var Redisserver *utils.RedisService

func init() {
	Redisserver = new(utils.RedisService)
}

// GetAnchorPriceInfo 函数根据主播ID和价格类型获取主播的价格信息。
func GetAnchorPriceInfo(anchorId int64, priceType int) (priceInfo *dtos.AnchorPriceInfoRes, err error) {
	videoCallPrice, err := models.GetVideo_call_priceByAnchorId(anchorId, priceType)
	if err != nil {
		return nil, err
	}

	if videoCallPrice == nil || videoCallPrice.Price == 0 {
		return nil, errors.New("价格信息不存在")
	}

	videoCallPriceConfig, err := models.GetVideo_call_price_configById(videoCallPrice.Price)
	if err != nil || videoCallPriceConfig == nil || videoCallPriceConfig.PayPrice == 0 {
		return
	}

	return &dtos.AnchorPriceInfoRes{
		PriceType:   videoCallPriceConfig.PriceType,
		PayPrice:    videoCallPriceConfig.PayPrice,
		AnchorPrice: videoCallPriceConfig.AnchorPrice,
	}, nil
}

// 如果是男性判断至少有1分钟使用的金币余额
func CheckMaleCallCost(viewerId, anchorId int64) (checked bool, payPrice int64, anchorPrice int64, err error) {
	checked = true
	// 获取女主播单价
	priceInfo, err := GetAnchorPriceInfo(anchorId, constants.PRICE_TYPE_ID_VIDEO_CALL)
	if err != nil {
		logs.Error("GetAnchorPriceInfo(%v, %v): %v", anchorId, constants.PRICE_TYPE_ID_VIDEO_CALL, err)
		checked = false
		return
	}

	payPrice = priceInfo.PayPrice
	anchorPrice = priceInfo.AnchorPrice

	// 获取用户金币余额
	respData, err := GetCocoUserbalance(fmt.Sprintf("%d", viewerId))
	if err != nil {
		logs.Error("调用coco接口获取用户金币余额报错：services.GetCocoUserbalance(%d): %v", viewerId, err)
		checked = false
		return
	}
	if respData.Result != 0 {
		logs.Error("调用coco接口获取用户金币余额报错：services.GetCocoUserbalance(%d): %v", viewerId, respData)
		checked = false
		return checked, payPrice, anchorPrice, errors.New("非法用户")
	}

	userBalance, _ := strconv.ParseInt(respData.Data.Balance, 10, 64)
	if userBalance < payPrice {
		checked = false
		return
	}

	return
}

// 根据用户id,获取不在用户黑名单中的在线可接单主播列表 userId:用户id,isRegion:是否按区域筛选 0:不按区域筛选 1:按区域筛选 2:筛选不在同一区域的主播
func GetOnlineTakingOrderAnchorsByUserId(userId int64, isRegion int, userSubRegionId int) (v []AnchorInfo, err error) {

	// 1. 查询用户的黑名单列表
	blackIds, err := models.GetBlackAnchorIdsByUserId(userId)
	if err != nil {
		return nil, err
	}

	o := orm.NewOrm()
	// 查询符合条件的主播数量，排除黑名单中的主播
	sql := `SELECT anchor_id,sub_region_id,status FROM anchor_info WHERE status IN (2, 3) AND disable_status = 0 AND live_model != 2`

	if isRegion == 1 {
		sql += " AND sub_region_id = " + fmt.Sprintf("%d", userSubRegionId)
	} else if isRegion == 2 {
		sql += " AND sub_region_id != " + fmt.Sprintf("%d", userSubRegionId)
	}

	if len(blackIds) > 0 {
		blackIdsStr := SliceToString(blackIds)
		sql += " AND anchor_id NOT IN (" + blackIdsStr + ") "
	}

	sql += " LIMIT 1000"
	_, err = o.Raw(sql).QueryRows(&v)
	if err != nil {
		return nil, err
	}

	return
}
